using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Chapter19.ViewModel
{
    using System.Data;
    using System.ComponentModel;
    using System.Collections.ObjectModel;
    using System.Threading;

    public class StoreDb
    {
        public Product GetProduct(int ID)
        {
            var ds = StoreDbDataSet.ReadDataSet();
            var productRow = ds.Tables["Products"].Select("ProductID = " + ID.ToString())[0];
            var product = new Product(
                (string)productRow["ModelNumber"],
                (string)productRow["ModelName"],
                (decimal)productRow["UnitCost"],
                (string)productRow["Description"],
                (int)productRow["CategoryID"],
                (string)productRow["CategoryName"],
                (string)productRow["ProductImage"]);
            return product;
        }

        public ICollection<Product> GetProducts()
        {
            var ds = StoreDbDataSet.ReadDataSet();
            var products = new ObservableCollection<Product>();
            foreach (DataRow productRow in ds.Tables["Products"].Rows)
            {
                products.Add(new Product((string)productRow["ModelNumber"],
                    (string)productRow["ModelName"], (decimal)productRow["UnitCost"],
                    (string)productRow["Description"], (int)productRow["CategoryID"],
                    (string)productRow["CategoryName"], (string)productRow["ProductImage"]));
            }
            return products;
        }

        public ICollection<Product> GetProductsSlow()
        {
            Thread.Sleep(TimeSpan.FromSeconds(5));
            return GetProducts();
        }

        public ICollection<Category> GetCategoriesAndProducts()
        {
            var ds = StoreDbDataSet.ReadDataSet();
            var relCategoryProduct = ds.Relations[0];
            var categories = new ObservableCollection<Category>();
            foreach (DataRow categoryRow in ds.Tables["Categories"].Rows)
            {
                var products = new ObservableCollection<Product>();
                foreach (DataRow productRow in categoryRow.GetChildRows(relCategoryProduct))
                {
                    products.Add(new Product(
                        productRow["ModelNumber"].ToString(),
                        productRow["ModelName"].ToString(), (decimal)productRow["UnitCost"],
                        productRow["Description"].ToString()));
                }
                categories.Add(new Category(categoryRow["CategoryName"].ToString(), products));
            }
            return categories;
        }

        public ICollection<Product> GetProductsFilteredWithLinq(decimal minimumCost)
        {
            var products = GetProducts();
            var matches = from product in products
                          where product.UnitCost >= minimumCost
                          select product;
            return new ObservableCollection<Product>(matches.ToList());
        }

        public ICollection<Category> GetCategories()
        {
            var ds = StoreDbDataSet.ReadDataSet();
            var categories = new ObservableCollection<Category>();
            foreach (DataRow categoryRow in ds.Tables["Categories"].Rows)
            {
                categories.Add(new Category(categoryRow["CategoryName"].ToString(), (int)categoryRow["CategoryID"]));
            }
            return categories;
        }
    }
}
